package com.cybertron.ironhide.manage.common.jstl;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.cybertron.ironhide.manage.domain.Category;

public class Tree extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Category> categories;
	private String theme;
	private String target;
	private String rel;
	private String href;
	private String accordionStart = "<div class=\"accordion\" fillSpace=\"sidebar\">";
	private String accordionHeader = "<div class=\"accordionHeader\"><h2><span>Folder</span>{0}</h2></div>";
	private String accordionContent = "<div class=\"accordionContent\">";
	private String treeHeader = "<ul class=\"tree treeFolder\">";
	private String treeContent = "<li><a href=\"{0}\"  target=\"{1}\" rel=\"{2}\" fresh=\"{3}\" external=\"{4}\" id={5}>{6}</a>";
	private StringBuffer buffResult;

	@Override
	public int doStartTag() throws JspException {
		if (categories == null || categories.size() == 0) {
			return SKIP_BODY;
		}
		buffResult = new StringBuffer();
		if ("accordion".equals(theme)) {
			createAccordion(categories);
		}
		if ("simple".equals(theme)) {
			createSimple(categories);
		}
		if ("tree".equals(theme)) {
			createTree(categories);
		}
		JspWriter out = pageContext.getOut();
		try {
			out.print(buffResult.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void createTree(List<Category> categories) {
		for (Category category : categories) {
			String href = MessageFormat.format(this.href, category.getId());
			buffResult.append(MessageFormat.format(treeContent, href, target,
					MessageFormat.format(rel, category.getId()), true, false,
					category.getId(), category.getTitle()));
			if (category.getChilds() != null
					&& category.getChilds().size() != 0) {
				buffResult.append("<ul>");
				createTree(category.getChilds());
				buffResult.append("</ul>");
			}
			buffResult.append("</li>");
		}
	}

	public void createSimple(List<Category> categories) {
		buffResult.append(treeHeader);
		createTree(categories);
		buffResult.append("</ul>");
	}

	public void createAccordion(List<Category> categories) {
		buffResult.append(accordionStart);
		for (Category category : categories) {
			buffResult.append(MessageFormat.format(accordionHeader,
					category.getTitle()));
			buffResult.append(accordionContent);
			buffResult.append(treeHeader);
			createTree(category.getChilds());
			buffResult.append("</ul>");
			buffResult.append("</div>");
		}
		buffResult.append("</div>");
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public void setItems(List<Category> items) {
		this.categories = items;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public void setHref(String href) {
		this.href = href;
	}

}
